package com.zsr.example.controller.front;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.entity.EduCollect;
import com.zsr.example.service.EduArticleService;
import com.zsr.example.service.EduCollectService;
import com.zsr.example.service.EduCourseService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.BusinessEnum;
import com.zsr.example.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-01-07
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/collect")
public class EduCollectController {

    @Autowired
    private EduCollectService collectService;

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduArticleService eduArticleService;

    @PostMapping("doCollect")
    public JSONObject collect(String targetId, Integer targetType, HttpServletRequest request) {
        CommonUtil.hasAllRequired(targetId, targetType);
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) throw new CommonJsonException(ErrorEnum.E_20011);

        QueryWrapper<EduCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("target_id", targetId);
        wrapper.eq("target_type", targetType);
        wrapper.eq("member_id", memberId);
        EduCollect collect = collectService.getOne(wrapper);
        boolean isCollect = true;
        if (!ObjectUtils.isEmpty(collect)) {//已收藏
            isCollect = false;
            collectService.removeById(collect.getId());

        } else {
            collect = new EduCollect();
            collect.setMemberId(memberId);
            collect.setTargetId(targetId);
            collect.setTargetType(targetType);
            collectService.saveOrUpdate(collect);
        }
        return CommonUtil.successJson(isCollect);
    }

    @GetMapping("getList")
    public JSONObject getCollectList(HttpServletRequest request, @RequestParam(required = false) Integer targetType) {
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) throw new CommonJsonException(ErrorEnum.E_20011);
        List<EduCollect> list = null;
        List result = null;
        if (!StringUtils.isEmpty(memberId)) {
            QueryWrapper<EduCollect> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("member_id", memberId);
            if (!ObjectUtils.isEmpty(targetType)) queryWrapper.eq("target_type", targetType);
            list = collectService.list(queryWrapper);

            if (!CollectionUtils.isEmpty(list)) {
                List<String> ids = list.stream().map(EduCollect::getTargetId).collect(Collectors.toList());
                if (targetType == BusinessEnum.COURSE.getType()) {
                    result = (List) courseService.listByIds(ids);
                } else if (targetType == BusinessEnum.ARTICLE.getType()) {
                    result = (List) eduArticleService.listByIds(ids);
                }
            }

        }
        return CommonUtil.successJson(result);

    }
}

